/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.input;

import com.sleekice.core.SleekCore;
import com.sleekice.core.Updatable;
import com.sleekice.util.List;
import com.sleekice.util.ListNode;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class MouseInput extends MouseAdapter implements Updatable {

    private List<Hoverable> objects_;
    private int mouseX_;
    private int mouseY_;
    private Clickable lastClickedItem_;
    private boolean leftButtonPressed_;
    private boolean rightButtonPressed_;
    /**
     * If not layerBased, all components with mouse area within the mouse
     * position will be processed. Otherwise only the first detected which is
     * also the closest to the top of list will be processed.
     * FIXME: still not completely tested
     */
    private boolean layerBased = true;
    public MouseMotionAdapter mmAdapter =
        new MouseMotionAdapter() {

        @Override
        public void mouseMoved(MouseEvent me) {
            mouseX_ = me.getX();
            mouseY_ = me.getY();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            mouseX_ = me.getX();
            mouseY_ = me.getY();
            //System.out.println("dragging");
            //System.out.println(me.getX()+","+me.getY());
        }
    };

    public MouseInput() {
        objects_ = new List<Hoverable>();
        leftButtonPressed_ = false;
        rightButtonPressed_ = false;
    }

    public boolean isLeftButtonPressed() {
        return leftButtonPressed_;
    }

    public boolean isRightButtonPressed() {
        return rightButtonPressed_;
    }

    @Override
    public void mousePressed(MouseEvent me) {
        mouseX_ = me.getX();
        mouseY_ = me.getY();
        switch (me.getButton()) {
            case MouseEvent.BUTTON1:
                leftButtonPressed_ = true;
                break;
            case MouseEvent.BUTTON3:
                rightButtonPressed_ = true;
                break;
        }

        ListNode<Hoverable> node = objects_.getFirst();
        while (node != null) {
            if (node.getValue() instanceof Clickable) {
                Clickable temp = (Clickable) node.getValue();
                if (temp.getMouseArea().contains(mouseX_, mouseY_)) {
                    if (!temp.isMousePressed()) {
                        temp.mousePress();
                        if (layerBased) {
                            break;
                        }
                    }
                }
            }
            node = node.next();
        }
    }

    public void addTrigger(Hoverable object) {
        objects_.add(object);
    }

    public void removeTrigger(Hoverable object) {
        objects_.delete(object);
    }

    public void assignTo(SleekCore sleekcore) {
        sleekcore.addMouseListener(this);
        sleekcore.addMouseMotionListener(mmAdapter);
    }

    public Clickable getLastClicked() {
        return lastClickedItem_;
    }

    public int getMouseX() {
        return mouseX_;
    }

    public int getMouseY() {
        return mouseY_;
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        mouseX_ = me.getX();
        mouseY_ = me.getY();
        switch (me.getButton()) {
            case MouseEvent.BUTTON1:
                leftButtonPressed_ = false;
                break;
            case MouseEvent.BUTTON3:
                rightButtonPressed_ = false;
                break;
        }


        boolean clicked = false;
        ListNode<Hoverable> node = objects_.getFirst();
        while (node != null) {
            if (node.getValue() instanceof Clickable) {
                Clickable temp = (Clickable) node.getValue();
                if (temp.getMouseArea().contains(mouseX_, mouseY_)) {
                    if (temp.isMousePressed()) {
                        temp.mouseClick();
                        lastClickedItem_ = temp;
                        clicked = true;
                        if (layerBased) {
                            break;
                        }
                    }
                }
                else {
                    if (temp.isMouseOver()) {
                        temp.mouseLeave();
                    }
                    if (temp.isMousePressed()) {
                        temp.mouseRelease();
                    }
                }
            }
            node = node.next();
        }
        if (!clicked) {
            lastClickedItem_ = null;
        }
    }

    public void update(double elapsedTime) {
        /* process mouse rollover here to be in sync with the framerate */
        ListNode<Hoverable> node = objects_.getFirst();
        while (node != null) {
            if (node.getValue() instanceof Clickable) {
                Clickable temp = (Clickable) node.getValue();
                if (temp.getMouseArea().contains(mouseX_, mouseY_)) {
                    if (!temp.isMouseOver()) {
                        temp.mouseOver();
                    }
                }
                else {
                    if (temp.isMouseOver()) {
                        temp.mouseLeave();
                    }
                    if (temp.isMousePressed()) {
                        temp.mouseRelease();
                    }
                }
            }
            else {
                Hoverable temp = node.getValue();
                if (temp.getMouseArea().contains(mouseX_, mouseY_)) {
                    if (!temp.isMouseOver()) {
                        temp.mouseOver();
                    }
                }
                else {
                    if (temp.isMouseOver()) {
                        temp.mouseLeave();
                    }
                }
            }
            node = node.next();
        }
    }
}